﻿using DevelopAssistant.Core.ToolBar;
using DevelopAssistant.Service;
using ICSharpCode.WinFormsUI.Docking;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing; 
using System.Text; 
using System.Windows.Forms;

namespace DevelopAssistant.Core.DBMS
{
    public partial class DeleteTableDocument : DockContent, IDocumentContent
    {
        TreeNode TNode = null;
        LeftMenuBar OwnerForm = null;
        DataBaseServer SServer = null;

        public DeleteTableDocument()
        {
            InitializeComponent();
        }

        public DeleteTableDocument(TreeNode Node, DataBaseServer Server, LeftMenuBar leftBar)
            : this()
        {
            TNode = Node;
            SServer = Server;
            OwnerForm = leftBar;
            txtTableName.Text = TNode.Text;
        }

        public void FindAndReplace(FindAndReplaceRequest request)
        {
            
        }

        private void DeleteTableDocument_Load(object sender, EventArgs e)
        {
            using (var db = Utility.GetAdohelper(SServer))
            {
                var dt = db.GetDependencies(txtTableName.Text, 0);               
                foreach (DataRow row in dt.Rows)
                {
                    string name = row["ObjectName"] + "(" + row["ObjectType"] + ")";
                    this.listBox1.Items.Add(name);
                }
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this,
                "确定要删除 " + this.txtTableName.Text + " 吗?", "信息提示",
                MessageBoxButtons.OKCancel).Equals(DialogResult.OK))
            {
                string strSql = "DROP TABLE ";
                strSql +=SnippetBase.GetTableName(this.txtTableName.Text, SServer.ProviderName) + ";";

                using (var db = Utility.GetAdohelper(SServer))
                {
                    db.Execute(CommandType.Text, strSql, null);
                }

                OwnerForm.SetSelectedNode(TNode.Parent);
                OwnerForm.RefreshDataBase();
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
